package com.lee.lease.web.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.RootNameLookup;
import com.lee.lease.common.custom.exception.LeaseException;
import com.lee.lease.common.result.ResultCodeEnum;
import com.lee.lease.model.entity.*;
import com.lee.lease.model.enums.ItemType;
import com.lee.lease.model.enums.RedisConstant;
import com.lee.lease.web.admin.mapper.*;
import com.lee.lease.web.admin.service.*;
import com.lee.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.lee.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.lee.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.lee.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.lee.lease.web.admin.vo.fee.FeeValueVo;
import com.lee.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FeeKeyMapper feeKeyMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Override
    public void saveApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean update = apartmentSubmitVo.getId()!=null;
        super.saveOrUpdate(apartmentSubmitVo);
        Long id = apartmentSubmitVo.getId();
        if (update) {
            //根据id删除公寓设施
            LambdaQueryWrapper<ApartmentFacility> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(queryWrapper);

            //根据id删除公寓标签
            LambdaQueryWrapper<ApartmentLabel> labelInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            labelInfoLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(labelInfoLambdaQueryWrapper);

            //根据id删除公寓杂费
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

            //根据id删除公寓图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            redisTemplate.delete(RedisConstant.ADMIN_APARTMENT_PREFIX + id);
        }

        //增加新的设施信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().facilityId(facilityInfoId).apartmentId(id).build();
                apartmentFacilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

        //增加新的标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(id).labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //增加新的杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build();
                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }


        //增加新的图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(id);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

    }

    @Override
    public IPage<ApartmentItemVo> findPageApartmentItemVo(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> pageApartmentItemVo = apartmentInfoMapper.findPageApartmentItemVo(page, queryVo);
        return pageApartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        String key = RedisConstant.ADMIN_APARTMENT_PREFIX + id;
        ApartmentDetailVo apartmentDetailVo = (ApartmentDetailVo)redisTemplate.opsForValue().get(key);
        if (apartmentDetailVo!=null) {
            return apartmentDetailVo;
        }else {
            ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
            if (apartmentInfo==null) {
                return null;
            }

            List<GraphVo> graphVoList = graphInfoMapper.selectByItemtyeAndId(ItemType.APARTMENT,id);
            List<LabelInfo> labelInfoList = labelInfoMapper.selectByApartmentId(id);
            List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByApartmentId(id);
            List<FeeValueVo> feeValueVoList = feeKeyMapper.findByApartmentId(id);

            apartmentDetailVo = new ApartmentDetailVo();
            BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
            apartmentDetailVo.setGraphVoList(graphVoList);
            apartmentDetailVo.setLabelInfoList(labelInfoList);
            apartmentDetailVo.setFacilityInfoList(facilityInfoList);
            apartmentDetailVo.setFeeValueVoList(feeValueVoList);
            redisTemplate.opsForValue().set(key,apartmentDetailVo);
            return apartmentDetailVo;
        }
    }

    @Override
    public Integer removeApartmentById(Long id) {
        //判断公寓下是否有房间,有房间的话就不能删除这个房间,抛出还有自己未删除的异常
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(queryWrapper);
        if (count > 0 ){
                throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        //公寓中没有房间,删除公寓以及公寓的图片,配套关系,标签关系,杂费值关系
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,1).eq(GraphInfo::getItemId,id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityMapper.delete(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelMapper.delete(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueMapper.delete(apartmentFeeValueLambdaQueryWrapper);

        apartmentInfoMapper.deleteById(id);
        redisTemplate.delete(RedisConstant.ADMIN_APARTMENT_PREFIX + id);
        return null;
    }
}




